package com.jojo.android.zxlib.base;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.appcompat.app.AppCompatActivity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;


import com.jojo.android.zxlib.R;
import com.jojo.android.zxlib.base.mvp.BasePresenter;
import com.jojo.android.zxlib.tool.ZxActivityTool;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.jojo.android.zxlib.tool.ZxToast;

import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class BaseActivity<P extends BasePresenter>
        extends AppCompatActivity implements Handler.Callback {
    protected P presenter = null;
    private final String TAG = "BaseActivity";
    protected View rootView;
    Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            //拦截
            if (intercept()) {
                return;
            }
            ZxActivityTool.pushActivity(this);
            systemBar();
            rootView = LayoutInflater.from(this).inflate(getLayoutId(), null);
            setContentView(rootView);
            unbinder = ButterKnife.bind(this);
            presenter = initPresenter();
            initUI();
            regEvent();
            initData();
            refresh();
        } catch (Exception e) {
            ZxLogTool.err(e);
        }

    }

    protected boolean intercept() {
        return false;
    }

    public void refresh() {

    }

    protected abstract P initPresenter();

    protected BaseActivity getActivity() {
        return this;
    }

    /**
     * 设置沉浸状态栏，如果不需要可复写
     */
    protected void systemBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (isImmerse()) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            } else {
                getWindow().setStatusBarColor(getResources().getColor(getStatusBarResource()));
            }
        }
    }

    protected boolean isImmerse() {
        return false;
    }

    /**
     * 设置全沉浸式状态栏，返回一个状态栏高度，可以通过这个高度设置外边距
     *
     * @return
     */
    public int setImmerse(boolean b) {
        if (b) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
            int result = 0;
            int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = this.getResources().getDimensionPixelSize(resourceId);  //获取系统栏高度
            }
            return result;
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
            return 0;
        }
    }

    /**
     * 获取状态栏的颜色
     *
     * @return
     */
    protected int getStatusBarResource() {
        return R.color.white;
    }


    /**
     * 设置状态栏字体颜色
     *
     * @param lightMode 是否高亮
     */
    public void setStatusBarLightMode(boolean lightMode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //如果是6.0以上将状态栏文字改为黑色，并设置状态栏颜色
            getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            if (lightMode) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

            } else {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            }
            //fitsSystemWindow 为 false, 不预留系统栏位置.
            ViewGroup mContentView = getWindow().findViewById(Window.ID_ANDROID_CONTENT);
            View mChildView = mContentView.getChildAt(0);
            if (mChildView != null) {
                ViewCompat.setFitsSystemWindows(mChildView, true);
                ViewCompat.requestApplyInsets(mChildView);
            }
        }
    }

    /**
     * 返回布局ID
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 初始化UI
     */
    protected abstract void initUI();

    protected abstract void initData();

    protected abstract void regEvent();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ZxActivityTool.removeActivity(this);
        if (unbinder != null)
            unbinder.unbind();
        if (presenter != null)
            presenter.destroy();
    }

    @Override
    public boolean handleMessage(Message message) {
        return false;
    }


    @Override
    public void finish() {
        onFinishBefore();
        super.finish();
    }

    /**
     * @return false正常finish, true拦截finish
     */
    public boolean onFinishBefore() {
        return false;
    }


    private long systime;

    @Override
    public void onBackPressed() {
        if (doubleClickToExitSwitch()) {
            if (System.currentTimeMillis() - systime < 1000 * 1.5) {
                super.onBackPressed();
            }
            ZxToast.showBottomToastLight("再按一次退出");
            systime = System.currentTimeMillis();
        } else {
            super.onBackPressed();
        }
    }

    /**
     * 双击退出的开关，重写更改
     *
     * @return
     */
    public boolean doubleClickToExitSwitch() {
        return false;
    }


}
